const {
    getLableInfo,
    editlable,
    deletelable,
    getonelable,
    addlable,
    searchlables,
    getcount,
    deleteAllLable,
    getAllLable,
    ServerSelectlable
} = require("../serve/lable")
const Response = require("../util/respanse.js")
/* 分页获取标签 */
const getLable = async (req, res) => {
    try {
        let { pageIndex, pageSize } = req.query;
        console.log(pageIndex, pageSize);
        let ss = await getLableInfo(pageIndex, pageSize);
        console.log(ss.msg);
        if (ss.code) {
            Response.successresponse(res, {
                data: ss.data,
                message: ss.msg,
                code: 200
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "查询失败",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 编辑单个标签 */
const editLable = async (req, res) => {
    try {
        let { lablename, lable_id } = req.body
        /* 查询数据库 */
        let data = await getonelable(null, lable_id)
        console.log(data);
        if (data.code) {
            let isok = await editlable(lablename, lable_id)
            console.log(isok.msg);
            if (isok.msg) {
                Response.successresponse(res, {
                    code: 200,
                    msg: "修改成功",
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "修改失败",
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "没有标签",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "没有标签",
            err: error
        })
    }
}
/* 删除标签  lable_id 标签id*/
const deleteLable = async (req, res) => {
    try {
        let { lable_id } = req.body
        console.log(lable_id);
        let data = await deletelable(lable_id);
        console.log(data.msg);
        if (data.code) {
            Response.successresponse(res, {
                code: 200,
                message: "删除成功",
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: data.msg,
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 400,
            msg: "删除失败",
            err: error
        })
    }
}
/* 查看标签的唯一性 */
const isUniqut = async (req, res, next) => {
    try {
        let lablename = req.body.lablename
        /* 查询数据库 */
        let data = await getonelable(lablename)
        console.log(data.msg);
        /* 获取成功 */
        if (data.code) {
            /* 查询到了 */
            Response.errorresponse(res, {
                code: 400,
                msg: "标签名已存在",
            })
        } else {
            next()
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 添加标签 */
const addlables = async (req, res) => {
    try {
        let { lablename } = req.body

        let resdata = await addlable(lablename)
        console.log(resdata.msg);
        if (resdata.code) {
            Response.successresponse(res, {
                code: 200,
                message: "添加成功",
                data: {
                    lablename: resdata.data.lable_name,
                    id: resdata.data.id
                }
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "添加失败",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 获取标签数量 */
const getlablecount = async (req, res) => {
    try {
        let dataa = await getcount();

        if (dataa.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: dataa.data
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 400,
            msg: "获取失败",
            err: error
        })
    }
}
/* 关键词分页搜索标签 */
const searchlable = async (req, res) => {
    try {
        let { keyword, pageinfo } = req.body;
        let ress = await searchlables(keyword, pageinfo);
        console.log(ress);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "查询成功",
                data: {
                    data: ress.data,
                    num: ress.num
                },

            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "查询失败",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 批量删除标签 */
const deleteAllLableList = async (req, res) => {
    try {
        let { lablelist } = req.body
        let ress = await deleteAllLable(lablelist);
        let num=0;
        let isok = false
        console.log(ress);
        ress.forEach(item =>{
            if(item.code){
                num++
            }else{
                isok=false
            }
        })
        if (isok) {
            Response.successresponse(res, {
                code: 200,
                message: "全部删除成功",
                data: {
                    num
                }
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: "部分删除成功",
                data: {
                    num
                }
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res,{
            code: 500,
            msg: "批量删除标签出错了",
            err: error
        })
    }
}
/* 一次性全部获取标签 */
const oneTimegetlables =async (req, res) =>{
    try {
           let ress = await getAllLable();
           if(ress.code){
             Response.successresponse(res,{
                code:200,
                message:"获取标签成功",
                data:ress.data
             })
           }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res,{
            code: 500,
            msg: "一次性获取标签出错了",
            err: error
        })
    }
}
const selectlable =async (req, res) =>{
    try {
        let {lable_id,page,pageSize} = req.body 
        console.log(lable_id,page,pageSize);
        let ress = await ServerSelectlable(lable_id,page,pageSize);
        if(ress.code){
          Response.successresponse(res,{
             code:200,
             message:"获取标签成功",
             data:ress.data
          })
        }else{
            Response.errorresponse(res, {
                code: 400,
                msg: "暂无数据",
            })
        }
 } catch (error) {
     console.log(error);
     Response.errorresponse(res,{
         code: 500,
         msg: "出错了",
         err: error
     })
 }
}
module.exports = {
    getLable,
    editLable,
    deleteLable,
    isUniqut,
    addlables,  
    searchlable,
    getlablecount,
    deleteAllLableList,
    oneTimegetlables,
    selectlable
}